package com.ley.yikang.controller;

import com.ley.yikang.Annotation.PassToken;
import com.ley.yikang.Annotation.UserLoginToken;
import com.ley.yikang.entity.Child;
import com.ley.yikang.entity.ChildAndElderBind;
import com.ley.yikang.entity.Elder;
import com.ley.yikang.service.ChildService;
import com.ley.yikang.service.ElderAndChildBindService;
import com.ley.yikang.service.ElderService;
import com.ley.yikang.util.ResultData;
import com.ley.yikang.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/child")
public class ChildController {
    private final ChildService childService;
    private final ElderAndChildBindService elderAndChildBindService;

    private final ElderService elderService;

    @Autowired
    public ChildController(ChildService childService, ElderAndChildBindService elderAndChildBindService, ElderService elderService) {
        this.childService = childService;
        this.elderAndChildBindService = elderAndChildBindService;
        this.elderService = elderService;
    }

    @PassToken
    @PostMapping("/register")
    public ResultData<Boolean> addUser(@RequestBody Child child) {
        //判断手机号是否合法
        if (!child.getPhone().matches("1(3[0-9]|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\\d{8}")) {
            System.out.println("手机号不合法！");
            return ResultData.fail("手机号不合法！");
        }
        //判断用户是否存在
        Child child1 = childService.findChild(child.getPhone());
        if (child1 == null) {
            childService.addChild(child);
            System.out.println("子女用户注册成功！");
            return ResultData.success(true, "用户注册成功！");
        } else {
            System.out.println("用户注册失败,该手机号已被注册！");
            return ResultData.fail("用户注册失败,该手机号已被注册！");
        }
    }


    @PassToken
    @PostMapping(value = "/login")
    public ResultData<String> login(@RequestBody Child child) {
        Child child1 = childService.findChild(child.getPhone());
        if (child1 != null) {
            //系统中有这样的用户
            if (child1.getPassword().equals(child.getPassword())) {
                System.out.println("找到子女用户！登陆成功！");
                //返回token
                return ResultData.success(TokenUtil.getChildToken(child1), "用户登陆成功！");
            } else {
                System.out.println("密码错误！登陆失败！");
                return ResultData.fail("密码错误！登陆失败！");
            }
        } else
            return ResultData.fail("用户名或密码错误！");
    }

    @UserLoginToken
    @DeleteMapping("/delChild")
    public ResultData<Boolean> delete(@RequestBody Child child) {
        //判断用户是否存在
        if (childService.findChildById(child.getId()) == null) {
            System.out.println("没有找到子女用户！删除失败！");
            return ResultData.fail("没有找到该用户！删除失败！");
        }
        //先删除子女绑定老人信息
        elderAndChildBindService.delChildAndElder(child.getId());
        // 删除子女信息
        childService.delChild(child.getId());
        System.out.println("删除子女用户：" + child.getId());
        return ResultData.success(true, "用户删除成功！");
    }

    @UserLoginToken
    @PostMapping("/updateChild")
    public ResultData<Child> update(@RequestBody Child child) {
        //判断用户是否存在
        if (childService.findChildById(child.getId()) == null) {
            System.out.println("没有找到子女用户！修改失败！");
            return ResultData.fail("没有找到该用户！修改失败！");
        }
        //判断是否有使用该手机号的用户
        if (childService.findChild(child.getPhone()) != null && childService.findChild(child.getPhone()).getId() != child.getId()) {
            System.out.println("手机号已被使用！修改失败！");
            return ResultData.fail("手机号已被使用！修改失败！");
        }
        childService.updateChild(child);
        System.out.println("修改子女用户：" + child.getPhone());
        return ResultData.success(child, "用户修改成功！");
    }

    @UserLoginToken
    @GetMapping("/findAllChild")
    public ResultData<List<Child>> findAll(@RequestParam(required = false) String childName,
                                           @RequestParam(required = false) String phone,
                                           @RequestParam(required = false) Integer communityId) {
        List<Child> children = childService.findAll(childName, phone, communityId);
        //如果查询出来的数组包含id为0的用户则删除
        for (int i = 0; i < children.size(); i++) {
            if (children.get(i).getId() == 0) {
                children.remove(i);
            }
        }
        System.out.println("查询子女用户！");
        return ResultData.success(children, "用户查询成功！");
    }

    @UserLoginToken
    @GetMapping("/findChildByToken")
    public ResultData<Child> findChildByToken(String token) {
        int id = TokenUtil.getTokenUserId(token);
        Child child = childService.findChildById(id);
        if (child == null) {
            System.out.println("没有找到该子女用户！");
            return ResultData.fail("没有找到该用户！");
        }
        System.out.println("用户信息查询成功！");
        return ResultData.success(child, "用户信息查询成功！");
    }

    @UserLoginToken
    @PostMapping("/bindChildAndElder")
    public ResultData<Boolean> bindChildAndElder(@RequestBody ChildAndElderBind childAndElderBind) {
        //判断子女用户是否存在
        Child child = childService.findChildById(childAndElderBind.getChildId());
        if (child == null) {
            System.out.println("没有找到子女用户！绑定失败！");
            return ResultData.fail("没有找到该子女用户！绑定失败！");
        }
        //判断老人用户是否存在
        Elder elder = elderService.findElderById(childAndElderBind.getElderId());
        if (elder == null) {
            System.out.println("没有找到老人用户！绑定失败！");
            return ResultData.fail("没有找到该老人用户！绑定失败！");
        }
        //判断是否已经绑定
        if (elderAndChildBindService.getChildAndElderInfo(childAndElderBind.getChildId(), childAndElderBind.getElderId()) != null) {
            System.out.println("已和该用户绑定！绑定失败！");
            return ResultData.fail("您已和该用户绑定！");
        }
        //判断绑定数量
        if (elderAndChildBindService.getChildAndElder(childAndElderBind.getChildId()).size() >= 2) {
            System.out.println("绑定老人数量已超过两位！绑定失败！");
            return ResultData.fail("您的绑定老人数量已超过两位！");
        }
        elderAndChildBindService.bindChildAndElder(childAndElderBind.getChildId(), childAndElderBind.getElderId());
        System.out.println("绑定成功！");
        return ResultData.success(true, "绑定成功！");
    }

    @UserLoginToken
    @GetMapping("/getChildAndElder")
    public ResultData<List<Elder>> getChildAndElder(int childId) {
        //判断用户是否存在
        if (childService.findChildById(childId) == null) {
            System.out.println("没有找到子女用户！查找失败！");
            return ResultData.fail("没有找到该子女用户！查找失败！");
        }
        return ResultData.success(elderAndChildBindService.getChildAndElder(childId), "查询成功！");
    }

    @UserLoginToken
    @PostMapping("/unbindChildAndElder")
    public ResultData<Boolean> unbindChildAndElder(@RequestBody ChildAndElderBind childAndElderBind) {
        //判断用户是否存在
        if (childService.findChildById(childAndElderBind.getChildId()) == null) {
            System.out.println("没有找到子女用户！解绑失败！");
            return ResultData.fail("没有找到该子女用户！解绑失败！");
        }
        elderAndChildBindService.unbindChildAndElder(childAndElderBind.getChildId(), childAndElderBind.getElderId());
        System.out.println("解绑成功！");
        return ResultData.success(true, "解绑成功！");
    }

}
